/*
Written by Best Flash Solutions, Inc  
See:
	http://www.bestflashsolutions.com
Contact me: contact@bestflashsolutions.com
*/

/**
 * Control Red5 application
 */
package com.bestflashsolutions.red5Streaming
{
	import com.bestflashsolutions.CommonUtils;
	
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	
	public class AppControl
	{
		var nc: NetConnection;
		public var objInformation: MyObject;
		public var sServer: String;
		public var sApp: String;
		public var sUsername: String;
		public var onComplete: Function;

		//Callback function
		public function setCompleteHandler(_handler: Function) {
			nc.removeEventListener(NetStatusEvent.NET_STATUS, onComplete);
			onComplete = _handler;
			nc.addEventListener(NetStatusEvent.NET_STATUS, onComplete);	
		}
		
		public function AppControl(server: String, app:String)
		{
			//Store server name and application name
			sServer = server;
			sApp = app;	
		}
		
		public function connect():void
        {
            // new netconnection to red5 server
            nc = new NetConnection();
            // set encoding to old amf
            nc.objectEncoding = ObjectEncoding.AMF0;
            // netstatus event listening
            nc.addEventListener(NetStatusEvent.NET_STATUS, onComplete);	
			//nc.client = new EventManager(this);//this;
           	nc.connect("rtmp://" +  sServer + "/" + sApp, "app", "", sUsername);
			CommonUtils.log("AppControl:Connect...");
        }
        
        public function disconnect():void {
        	nc.close();
        	nc = null;
        }

	}
}